﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using SQLServerHealthCheck.Model;

namespace SQLServerHealthCheck.SQLServer.AutoFixes
{
    public class AutoFixFactory
    {
        public static List<AutoFixBase> GetAllFixes()
        {
            List<AutoFixBase> fixes = new List<AutoFixBase>();

            //Add if constructions to dynamically add or remove checks for example with checkboxes in the GUI
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());
  

                Assembly asm = Assembly.Load("SQLServerHealthCheck.SQLServer");

                var qry = from t in asm.GetTypes()
                          where t.IsClass && t.Namespace == "SQLServerHealthCheck.SQLServer.AutoFixes" && t.BaseType.Name == "AutoFixBase"
                          select t;

                var enabled = from a in dc.IssueTypes
                              where a.Fixable == true && a.Enabled == true
                              select a.IssueTypeId;

                foreach (var item in qry)
                {
                    AutoFixBase fix = (AutoFixBase)Activator.CreateInstance(item);
                    if (enabled.Contains(fix.IssueTypeId))
                    {

                        fixes.Add(fix);
                    }
                }
                //fixes.Add(new ServiceFixOleAutomationEnabled()); //2005
                //fixes.Add(new ServiceFixUserConnectionsNotDefault());//2008
                //fixes.Add(new ServiceFixXPcmdshellEnabled());//2006
                //fixes.Add(new DatabaseFixStateOffline());//3004
                //fixes.Add(new DatabaseFixDatabaseSnapshots());//3019
                //fixes.Add(new DatabaseFixAutoShrinkStateOn());//3011
                //fixes.Add(new DatabaseFixCompatibilityLevel());//3006
                //fixes.Add(new ServiceFixMemoryLimit());//2019
                //fixes.Add(new ServiceFixParallelism());//2018
            }
            catch (Exception ex)
            { 
            
                  
             
            
            }
            return fixes;
        }
    }

}
